package com.smartshopping.iu;

import java.io.IOException;
import java.util.ArrayList;

import com.smartshopping.model.BillItem;
import com.smartshopping.model.DataBaseHelper;
import com.smartshopping.model.Item;

import android.os.Bundle;
import android.os.Parcelable;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ListBillActivity extends Activity {

	private String sql;
	private DataBaseHelper myDbHelper;
	private ArrayList<BillItem> listBill = new ArrayList<BillItem>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_bill);
		connectionDatabase();
		createTheListBill();
	}

	public void connectionDatabase() {
		myDbHelper = new DataBaseHelper(this);
		try {
			myDbHelper.createDataBase(this);
			myDbHelper.openDataBase();
		} catch (IOException ioe) {
			throw new Error("Unable to create database");
		}
	}

	public void createTheListBill() {
		// Get the bill
		sql = "select * from bill order by _id DESC";
		System.out.println(sql);
		SQLiteDatabase mdbReader = myDbHelper.getReadableDatabase();
		Cursor mCursor = mdbReader.rawQuery(sql, null);

		int in = 0;
		if (mCursor != null && mCursor.moveToFirst()) {
			do {
				createTitleBill(mCursor, in);
				in++;
			} while (mCursor.moveToNext());
		}
		mCursor.close();
	}

	public void createTitleBill(Cursor mCursor, int index) {
		final int in = index;
		final Cursor mC = mCursor;
		final String date = mCursor.getString(1);
		LinearLayout layout = (LinearLayout) findViewById(R.id.bill_list_LinearLayout);
		layout.setOrientation(LinearLayout.VERTICAL); // Can also be done in xml
														// by
														// android:orientation="vertical"

		LinearLayout row = new LinearLayout(this);
		row.setOrientation(LinearLayout.HORIZONTAL);
		row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));
		LinearLayout row1 = new LinearLayout(this);
		row1.setOrientation(LinearLayout.HORIZONTAL);
		row1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));

		// Order and Date
		TextView textviewName = new TextView(this);
		textviewName.setLayoutParams(new LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		textviewName.setText(Integer.toString(index + 1) + " : "
				+ date);
		textviewName.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent myIntent1 = new Intent(ListBillActivity.this, BillViewActivity.class);
				myIntent1.putExtra("coopID", Integer.toString(listBill.get(in).getCoopId()));
				myIntent1.putExtra("marketID", Integer.toString(listBill.get(in).getMarketId()));
				myIntent1.putExtra("date", date);
				myIntent1.putParcelableArrayListExtra("list", listBill.get(in).getList());
				ListBillActivity.this.startActivityForResult(myIntent1, 2);
			}
	    });
		textviewName.setId(1 + (index * 2));
		row.addView(textviewName);

		// Note
		TextView textviewSum = new TextView(this);
		textviewSum.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		textviewSum.setText(mCursor.getString(4));
		textviewSum.setId(1 + (index * 2));
		row1.addView(textviewSum);

		layout.addView(row);
		layout.addView(row1);
		createListBill(mCursor);
	}

	public void createListBill(Cursor mC) {
		int billID = mC.getInt(0);
		int coopID = mC.getInt(2);
		int marketID = mC.getInt(3);
		ArrayList<Item> list = new ArrayList<Item>();
		sql = "select * from billdetail where BillID = " + Integer.toString(billID);
		System.out.println(sql);
		SQLiteDatabase mdbReader = myDbHelper.getReadableDatabase();
		Cursor mCursor = mdbReader.rawQuery(sql, null);
		
		if (mCursor != null && mCursor.moveToFirst()) {
			do {
				list.add( new Item(mCursor.getInt(2),mCursor.getString(6),mCursor.getInt(4),mCursor.getInt(3),mCursor.getInt(5)));
			} while (mCursor.moveToNext());
		}
		mCursor.close();
		listBill.add(new BillItem(list, coopID, marketID, billID));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.list_bill, menu);
		return true;
	}

}
